import {defineStore} from 'pinia'
import {audit, detail, getArticleList, getArticleUserList, remove, statusUser} from '@/service/article/index.js'
import getDate from '@/utils/dateChange.js'
import responseMessage from '@/utils/response.js'

const useArticleStore = defineStore('article', {
    state: () => ({
        articleList: '',
        detail: '',
        articleUserList: "",
    }),
    actions: {
        //获取文章列表
        async getArticleList(page, size, searchParams) {
            if (searchParams?.searchTime) {
                //改变事件
                const startTime = getDate(searchParams.searchTime[0])
                const endTime = getDate(searchParams.searchTime[1])
                searchParams.searchTime[0] = startTime
                searchParams.searchTime[1] = endTime
            }
            const res = await getArticleList(page, size, searchParams)
            this.articleList = res.data
        },

        //删除文章
        async removeAction(id) {
            const res = await remove(id)
            responseMessage(res)
        },
        //详情
        async detailAction(id) {
            const res = await detail(id)
            this.detail = res.data
        },
        //审核
        async auditAction(id) {
            const res = await audit(id)
        },

        //获取平台用户列表
        async getArticleUserListAction(page, size, searchParams) {
            const res = await getArticleUserList(page, size, searchParams)
            this.articleUserList = res.data
        },

        //启用/禁用
        async statusAction(userId,status) {
            const res = await statusUser(userId,status)
        },
    }
})

export default useArticleStore
